package com.jfinal.generator.service;

import java.util.List;

import javax.sql.DataSource;

import com.jfinal.generator.model.MetaBuilder;
import com.jfinal.generator.model.TableMeta;
import com.jfinal.plugin.activerecord.dialect.AnsiSqlDialect;
import com.jfinal.plugin.activerecord.dialect.Dialect;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.activerecord.dialect.OracleDialect;
import com.jfinal.plugin.activerecord.dialect.PostgreSqlDialect;
import com.jfinal.plugin.activerecord.dialect.SqlServerDialect;
import com.jfinal.plugin.activerecord.dialect.Sqlite3Dialect;
import com.jfinal.plugin.druid.DruidPlugin;

/**
 * GeneratorDemo
 */
public class GeneratorService {

	private String dataSource_url;
	private String dataSource_username;
	private String dataSource_password;
	// model 所使用的包名 (MappingKit 默认使用的包名)
	private String modelPackageName;
	// service 所使用包名
	private String servicePackageName;
	// service 文件保存路径
	private String serviceOutputDir;

	protected Dialect dialect = null;
	protected MetaBuilder metaBuilder;
	protected ServiceGenerator serviceGenerator;

	public GeneratorService(String dataSource_url, String dataSource_username, String dataSource_password, String servicePackageName, String modelPackageName) {
		this.dataSource_url = dataSource_url;
		this.dataSource_username = dataSource_username;
		this.dataSource_password = dataSource_password;
		this.modelPackageName = modelPackageName;
		this.servicePackageName = servicePackageName;
		this.serviceOutputDir = System.getProperty("user.dir") + "/src/main/java/" + servicePackageName.replace('.', '/');
	}

	public DataSource getDataSource() {
		String dataSource_driveClass = getDriveClass(dataSource_url);
		DruidPlugin druidPlugin = new DruidPlugin(dataSource_url, dataSource_username, dataSource_password, dataSource_driveClass);
		druidPlugin.start();
		return druidPlugin.getDataSource();
	}

	public void generate(String... table) {
		this.metaBuilder = new MetaBuilder(getDataSource());
		this.serviceGenerator = new ServiceGenerator(servicePackageName, serviceOutputDir, modelPackageName);
		metaBuilder.setDialect(getDialect(dataSource_url));
		metaBuilder.setTable(table);
		// 生成
		long start = System.currentTimeMillis();
		List<TableMeta> tableMetas = metaBuilder.build();
		if (tableMetas.size() == 0) {
			System.out.println("TableMeta 数量为 0，不生成任何文件");
			return;
		}

		serviceGenerator.generate(tableMetas);
		long usedTime = (System.currentTimeMillis() - start) / 1000;
		System.out.println("Generate complete in " + usedTime + " seconds.");
	}

	private Dialect getDialect(String dataSource_url) {
		if (dataSource_url.contains("postgresql")) {
			return new PostgreSqlDialect();
		} else if (dataSource_url.contains("sqlserver")) {
			return new SqlServerDialect();
		} else if (dataSource_url.contains("oracle")) {
			return new OracleDialect();
		} else if (dataSource_url.contains("sqlite")) {
			return new Sqlite3Dialect();
		} else if (dataSource_url.contains("mysql")) {
			return new MysqlDialect();
		} else {
			return new AnsiSqlDialect();
		}
	}

	private String getDriveClass(String dataSource_url) {
		if (dataSource_url.contains("postgresql")) {
			return "org.postgresql.Driver";
		} else if (dataSource_url.contains("sqlserver")) {
			return "com.microsoft.sqlserver.jdbc.SQLServerDriver";
		} else if (dataSource_url.contains("oracle")) {
			return "oracle.jdbc.OracleDriver";
		} else if (dataSource_url.contains("sqlite")) {
			return "org.sqlite.JDBC";
		} else if (dataSource_url.contains("mysql")) {
			return "com.mysql.cj.jdbc.Driver";
		} else {
			return "null";
		}
	}

}
